package de.bbl.android.fragment.NewsCenter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.bbl.android.BBLAndroidFragmentActivity;
import de.bbl.android.R;
import de.bbl.android.classes.RssItem;

@SuppressLint("ValidFragment")
public class NewsCenterExtendedFragment extends Fragment{

	private RssItem RssFeed;

	public static NewsCenterExtendedFragment newInstance(int index, RssItem rssFeed) {
		NewsCenterExtendedFragment f = new NewsCenterExtendedFragment(rssFeed);

        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }
	
	public NewsCenterExtendedFragment() {
		RssFeed = new RssItem("", "", "", "", "");
	}
	
	private NewsCenterExtendedFragment(RssItem rssFeed) {
		this.RssFeed = rssFeed;
	}

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }
     
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (container == null) {
            return null;
        }
		
		View myView = inflater.inflate(R.layout.expanded_layout_news, container, false);
        final TextView date = (TextView) myView.findViewById(R.id.storybox_date);
        final TextView titel = (TextView) myView.findViewById(R.id.storybox_titel);
        final TextView description = (TextView) myView.findViewById(R.id.storybox_description);
        final ImageView picture = (ImageView) myView.findViewById(R.id.storybox_picture);

        date.setText(RssFeed.getDate());
        if (RssFeed.getTitle() != null && RssFeed.getDescription() != null) {
        	titel.setText(RssFeed.getTitle());
        	description.setText(RssFeed.getDescription());
        }        
        
        String[] pics = RssFeed.getImagePath();

        if (pics[0] != null) {
        	String tempUrl = pics[0].substring(0,pics[0].lastIndexOf("/") -1);
        	tempUrl = tempUrl.substring(tempUrl.lastIndexOf("/") + 1) + "_" + pics[0].substring(pics[0].lastIndexOf("/") + 1); 

        	Bitmap bm = BitmapFactory.decodeFile(BBLAndroidFragmentActivity.bbl_temp_img_sd_path + tempUrl);
        	picture.setImageBitmap(bm);
            for (int i = 1; i < pics.length; i++ ) {
            	tempUrl = pics[i].substring(0,pics[i].lastIndexOf("/") -1);
            	tempUrl = tempUrl.substring(tempUrl.lastIndexOf("/") + 1) + "_" + pics[i].substring(pics[i].lastIndexOf("/") + 1); 
            	bm = BitmapFactory.decodeFile(BBLAndroidFragmentActivity.bbl_temp_img_sd_path + tempUrl);
            	LinearLayout imageScrollLayout = (LinearLayout)myView.findViewById(R.id.LayoutScrollView);
            	ImageView temp = new ImageView(getActivity());
            	temp.setImageBitmap(bm);
            	imageScrollLayout.addView(temp);
            }
        } else {
        	picture.setVisibility(View.INVISIBLE);
        }

    	return myView;
	}

}
